package com.csgc.camera.biz.other.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.csgc.camera.bean.LocationInfo
import com.csgc.camera.constants.MaskConfig

import com.csgc.camera.events.MaskLocationEvent
import com.csgc.camera.util.clickWithTrigger
import com.cysd.spsyxj.databinding.ItemLocationSelectBinding

import com.drake.channel.sendEvent

class LocationSelectAdapter(val context:Context,val list:ArrayList<LocationInfo>):Adapter<LocationSelectAdapter.LocationSelectHolder>() {

    private var checkPosition = -1
    var listener:OnLocationSelectListener?=null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LocationSelectHolder {
        return LocationSelectHolder(ItemLocationSelectBinding.inflate(LayoutInflater.from(context),parent,false))
    }

    override fun onBindViewHolder(holder: LocationSelectHolder, @SuppressLint("RecyclerView") position: Int) {
        val data = list[position]
        holder.binding.apply {

            tvLocationRoad.text = "${data.province}${data.city}${data.district}${data.street}"
            tvSnippet.text = data.snippet
            if (position == checkPosition){
                ivLocationChecked.visibility = View.VISIBLE
            }else{
                ivLocationChecked.visibility = View.GONE
            }

            root.clickWithTrigger {
                checkPosition = position
                notifyDataSetChanged()
                listener?.onSelect(data.snippet)

            }

        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class LocationSelectHolder(val binding:ItemLocationSelectBinding):ViewHolder(binding.root)
    interface OnLocationSelectListener{
        fun onSelect(snippet:String)
    }
}